const app = getApp()

Page({
  data: {
    list: [],
  },
  onLoad: function () {
    var that = this;
    if (!app.globalData.token) {
      tt.showToast({
        title: '请先登入',
        duration: 700,
      });
      setTimeout(function () {
        tt.navigateBack({
          delta: -1
        });
      }, 700)

    }
  },

  /**
   * 获取地址列表
   */
  getAdressList: function () {
    var _this = this;
    tt.showLoading({
      title: '加载中...',
      mask: true
    });

    tt.request({
      url: app.globalData.url + 'index.php?act=toutiao_member&op=get_address_list&key=' + encodeURI(app.globalData.token),
      data: {
        curpage: _this.data.curpage
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        tt.hideLoading({});
        if (res.data.code == 200) {
          _this.setData({
            list: res.data.datas.list,
          })

        } else {
          tt.showToast({
            title: res.data.datas.error,
            duration: 2000,
          });
        }
      },
      fail(res) {
        console.log(`request调用失败`);
      }
    })
  },

  onShow:function(){
    var that = this;
    that.getAdressList();
  },
  /**
  * 设置或取消默认地址
  */
  defaultAddress: function (e) {
    var _this = this;
    
    var address = _this.data.list;

    var is_default = 0;

    if (parseInt(e.currentTarget.dataset.is_default) == 0) {
      is_default = 1;
    } else {
      is_default = 0;
    }

    wx.request({
      url: app.globalData.url + 'index.php?act=toutiao_member&op=defaultAddress&key=' + encodeURI(app.globalData.token),
      dataType: 'json',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        address_id: e.currentTarget.dataset.id,
        is_default: is_default
      },
      success: function (res) {
        if (res.data.code == 200) {
          for (var i = 0; i < address.length; i++) {
            address[i].is_default = 0
            if (is_default == 1) {
              if (address[i].address_id == e.currentTarget.dataset.id) {
                address[i].is_default = 1
              }
            }
          }
          _this.setData({
            list: address
          });
 
        } else {
          wx: wx.showToast({
            title: res.data.datas.error,
            mask: true,
            icon: 'loading',
          });
        }
      }
    })
  },

  /**
   * 修改地址
   */
  editAddress: function(e) {
    wx.navigateTo({
      url: '/pages/my/editAddress?address_id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 删除地址
   */
  delAddress: function(e) {
    var _this = this;
    var address = _this.data.list
    if (parseInt(e.target.dataset.id) == parseInt(_this.data.address_id)) {
      app.globalData.isSelect = 2;
    }
    wx: wx.request({
      url: app.globalData.url + 'index.php?key=' + encodeURI(app.globalData.token),
      data: {
        act: 'member',
        op: 'address_del',
        address_id: e.target.dataset.id
      },
      method: 'POST',
      dataType: 'json',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        if (res.data.code == 200) {
          for (var i = 0; i < address.length; i++) {
            if (address[i].address_id == e.target.dataset.id) {
              address.splice(i, 1);
              _this.setData({
                list: address
              });
              return;
            }
          }
        }
      },
    })
  },
})
